<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Http\Controllers\Controller;
use App\Models\School;
use App\Models\SchoolCourse;
use App\Services\SchoolService;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class SchoolController extends AdminController
{
    //
    //添加学校
    public function  add(Request $request){

        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'student_num' => 'required|integer',
//            'phone' => 'regex:/^1[345789][0-9]{9}$/',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }

        $school =  SchoolService::getInstance()->getByName($request->name);
        if(!is_null($school)){
            return $this->fail(CodeResponse::AUTH_NAME_REGISTERED);
        }
        School::create($request->all());
        return $this->success();
    }
    // 上传
    public function upfile(Request $request) {
        $url = config('filesystems.disks.school.url');
        $default = config('filesystems.disks.school.default');
        $pic =  SchoolService::getInstance()->upfile($request,$url,$default,'school');
        return $this->success([
                'pic'=>$pic,
            ]
        );
    }
    //修改学校
    public function  update(Request $request){

        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
            'name' => 'required',
            'student_num' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $putData = $request->except(['_method','_token']);

        $school = new School();
        $school->find($request->id)->update($putData);
        return $this->success();
    }
    //列表
    public function  list(Request $request){

        $validator = Validator::make($request->all(), [
            'page' => 'integer',
            'limit' => 'integer',
            'name' => 'string',
            'sort' => 'string',
            'order' => 'string',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $name = $request->input('name');
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','desc');

        $list = SchoolService::getInstance()->listSchool($name, $page,$limit,$sort,$order);

        return $this->success($list);
    }
    //删除
    public function delete(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $school = School::find($request->id);
        $school->delete();
        return $this->success();
    }

    //学校课程
    public function  getSchoolCourse(Request $request){

        $validator = Validator::make($request->all(), [
            'school_id' => 'required|integer|digits_between:1,20|min:1',
            'coursesName' => 'string',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $school_id = $request->input('school_id');
        $coursesName = $request->input('coursesName');

        $schoollist = SchoolService::getInstance()->schoolCourse($school_id,$coursesName);
        $arr=[];
        foreach ($schoollist as $v){
            array_push($arr,$v->id);
        }
        //有作业的课程
        $worksCourseList = SchoolService::getInstance()->worksCourse($arr,$school_id);
        foreach ($schoollist as $v){
            $v->isUpdate = 0; //用户是否可以取消已有的课程
            foreach ($worksCourseList as $v2){
                if($v->id == $v2->course_id){
                    $v->isUpdate = 1;
                }
            }
        }
        return $this->success($schoollist);
    }

    //学校添加课程
    public function addSchoolCourse(Request $request){
        $validator = Validator::make($request->all(), [
            'school_id' => 'required|integer|digits_between:1,20|min:1',
            'course_ids' => 'required|json',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $schoolCourse = SchoolCourse::where('school_id',$request->school_id);
        $schoolCourse->delete();

        $arr = json_decode($request->course_ids);
        if(isset($arr) && count($arr) > 0 ) {
            foreach ($arr as $v) {
                SchoolCourse::create([
                    'school_id' => $request->school_id,
                    'course_id' => $v
                ]);
            }
        }
        return $this->success();
    }
    //学校删除课程
    public function delSchoolCourse(Request $request){
        $validator = Validator::make($request->all(), [
            'school_id' => 'required|integer|digits_between:1,20|min:1',
            'course_id' => 'required|integer|digits_between:1,20|min:1',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $list = SchoolService::getInstance()->isWorks($request->school_id,$request->course_id);
        if(is_null($list)){
            return $this->fail(CodeResponse::AUTH_SCHOOL_COURSE_WORKS_REGISTERED);
        }
        $schoolCourse = SchoolCourse::where('school_id',$request->school_id)->where('course_id',$request->course_id);
        $schoolCourse->delete();
        return $this->success();
    }
    //是否拥有学校班级课程
    public function ispossess(Request $request){
        $validator = Validator::make($request->all(), [
            'users_id' => 'required|integer|digits_between:1,20|min:1',
            'course_id' => 'required|integer|digits_between:1,20|min:1',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $users_id = $request->input('users_id');
        $course_id = $request->input('course_id');
        $list = SchoolService::getInstance()->ispossessSchoolCourse($users_id,$course_id);
        if(is_null($list)){
            return $this->fail(CodeResponse::AUTH_SCHOOL_CLASS_COURSE_REGISTERED);
        }
        return $this->success();
    }

}
